﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Mst;
using ESOL.Common;
using ESOL.Common.Entity;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors;

namespace ESOL.WinUI.Hr
{
    public partial class frmMaintainEmployeeQuery : BaseForm.frmBaseDialogyQueryForm
    {
        #region Variable
        private MstPersonEntity mSelectedPerson;
        public MstPersonEntity SelectedPerson
        {
            get { return mSelectedPerson; }
        }
        private List<MstPersonEntity> mSelectedPersonList;
        public List<MstPersonEntity> SelectedPersonList
        {
            get { return mSelectedPersonList; }
        }
        private MstDepartmentEntity mSelectedDepartment;
        private bool mShowAllPersons = false;
        private bool mMultiSelected = false;
        public bool MultiSelected 
        {
            get { return mMultiSelected; }
            set { mMultiSelected = value; }
        }
        #endregion

        #region Constructor
        public frmMaintainEmployeeQuery()
        {
            InitializeComponent();
        }
        public frmMaintainEmployeeQuery(bool showAllPersons)
        {
            InitializeComponent();
            mShowAllPersons = showAllPersons;
        }
        public frmMaintainEmployeeQuery(MstDepartmentEntity department)
        {
            InitializeComponent();
            mSelectedDepartment = department;
        }
        public frmMaintainEmployeeQuery(MstDepartmentEntity department, bool showAllPersons)
        {
            InitializeComponent();
            mSelectedDepartment = department;
            mShowAllPersons = showAllPersons;
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "PS", true);

            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colStatus, "PS", true);
        }

        private string GetCondition()
        {
            string condition = "";
            if (txtDepartment.Tag != null && txtDepartment.Tag.GetType() == typeof(MstDepartmentEntity))
            {
                MstDepartmentEntity department = (MstDepartmentEntity)txtDepartment.Tag;
                condition += " AND MP.DEPARTMENT_ID = '" + department.DepartmentId + "' ";
            }
            if (txtPosition.Tag != null && txtPosition.Tag.GetType() == typeof(MstPositionEntity))
            {
                MstPositionEntity position = (MstPositionEntity)txtPosition.Tag;
                condition += " AND POSITION.POSITION_ID = '" + position.PositionId + "' ";
            }
            if (txtSupervisor.Tag != null && txtSupervisor.Tag.GetType() == typeof(MstPersonEntity))
            {
                MstPersonEntity supervisor = (MstPersonEntity)txtDepartment.Tag;
                condition += " AND VMPP.SUPERVISOR = '" + supervisor.PersonId + "' ";
            }
            if (string.IsNullOrEmpty(txtPersonCode.Text) == false)
            {
                condition += " AND MP.PERSON_CODE LIKE '%" + txtPersonCode.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtPersonNameCn.Text) == false)
            {
                condition += " AND MP.NAME_CN LIKE '%" + txtPersonNameCn.Text + "%'";
            }
            if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            {
                condition += " AND MP.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            else
            {
                condition += " AND MP.STATUS != 'PS09' AND MP.STATUS != 'PS02'";
            }
            ///2013-01-16弃用，杨鹏飞，原因：员工仅仅有一个主职位，查询时根据条件查询所有员工，不再关联职位和权限
            //if (mShowAllPersons == false && CommonParameter.CurrentUser.PersonEntity != null)
            //{
            //    MstPersonEntity p = CommonParameter.CurrentUser.PersonEntity;
            //    condition = "AND (P.PERSON_ID = '" + p.PersonId + "' OR P2.PERSON_ID = '" + p.PersonId + "' OR P3.PERSON_ID = '" + p.PersonId + "' OR P4.PERSON_ID = '" + p.PersonId + "' OR P5.PERSON_ID = '" + p.PersonId + "')";
            //}
            //if (txtDepartment.Tag != null && txtDepartment.Tag.GetType() == typeof(MstDepartmentEntity))
            //{
            //    MstDepartmentEntity department = (MstDepartmentEntity)txtDepartment.Tag;
            //    condition += " AND P.DEPARTMENT_ID = '" + department.DepartmentId + "' ";
            //}
            //if (txtPosition.Tag != null && txtPosition.Tag.GetType() == typeof(MstPositionEntity))
            //{
            //    MstPositionEntity position = (MstPositionEntity)txtPosition.Tag;
            //    condition += " AND P.POSITION_ID = '" + position.PositionId + "' ";
            //}
            //if (txtSupervisor.Tag != null && txtSupervisor.Tag.GetType() == typeof(MstPersonEntity))
            //{
            //    MstPersonEntity supervisor = (MstPersonEntity)txtDepartment.Tag;
            //    condition += " AND P.SUPERVISOR = '" + supervisor.PersonId + "' ";
            //}
            //if (string.IsNullOrEmpty(txtPersonCode.Text) == false)
            //{
            //    condition += " AND P.PERSON_CODE LIKE '%" + txtPersonCode.Text + "%'";
            //}
            //if (string.IsNullOrEmpty(txtPersonNameCn.Text) == false)
            //{
            //    condition += " AND P.NAME_CN LIKE '%" + txtPersonNameCn.Text + "%'";
            //}
            //if (string.IsNullOrEmpty(cmbStatus.Text) == false && cmbStatus.SelectedValue != null)
            //{
            //    condition += " AND P.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            //}
            //else
            //{
            //    condition += " AND P.STATUS != 'PS09' AND P.STATUS != 'PS02'";
            //}
            return condition;
        }

        private void BindDataGrid()
        {
            string condition = GetCondition();
            DataTable dt = MstPersonBs.GetAllForCondition(condition, "", "");
            DataColumn dc=new DataColumn("SelectedFlag",typeof(bool));
            dc.DefaultValue=false;
            dt.Columns.Add(dc);
            gvMain.DataSource = dt;
            treePerson.DataSource = dt;
        }

        private void BindPersonTree()
        {
            treePerson.DataSource = null;
            List<MstPersonEntity> list = new List<MstPersonEntity>();
            if (mShowAllPersons == false)
            {
                list = MstPersonBs.GetPersonsAll(CommonParameter.CurrentUser.PersonId);
            }
            else
            {
                list = MstPersonBs.GetPersonsAll("");
            }
            treePerson.DataSource = list;
            treePerson.ExpandAll();
        }
        #endregion`

        #region Event
        protected override void OnShown(EventArgs e)
        {
            if (mSelectedDepartment != null)
            {
                txtDepartment.Tag = mSelectedDepartment;
                txtDepartment.Text = mSelectedDepartment.DepartmentName;
            }
            BindComboBox();
            //BindDataGrid();
            //BindPersonTree();
            if (mMultiSelected)
            {
                gridcolSelected.Visible = true;
                gridView1.OptionsBehavior.Editable = true;
                gridView1.OptionsBehavior.ReadOnly = true;
            }
            else
            {
                gridcolSelected.Visible = false;
                gridView1.OptionsBehavior.Editable = false;
                gridView1.OptionsBehavior.ReadOnly = true;
            }
            base.OnShown(e);
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }

        protected override void Click_Confirm()
        {
            if (!mMultiSelected)
            {
                if (mSelectedPerson == null)
                {
                    MessageBox.Show("请选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                mSelectedPersonList = new List<MstPersonEntity>();
                DataTable dt = gvMain.DataSource as DataTable;
                foreach (DataRow dr in dt.Rows)
                {
                    if ((bool)dr["SelectedFlag"])
                    {
                        mSelectedPersonList.Add(new MstPersonEntity(dr));
                    }
                }
                if (mSelectedPersonList.Count == 0)
                {
                    MessageBox.Show("请至少选择一个员工！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            this.DialogResult = DialogResult.OK;
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedPerson = new MstPersonEntity(dr);
        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (mMultiSelected)
            {
                return;
            }
            if (mSelectedPerson != null)
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void treePerson_AfterFocusNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
        {
            DataRow dr = ((DataRowView)treePerson.GetDataRecordByNode(e.Node)).Row;
            mSelectedPerson = new MstPersonEntity(dr);
        }

        private void treePerson_DoubleClick(object sender, EventArgs e)
        {
            if (mSelectedPerson != null)
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        private void txtDepartment_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainDepartmentQuery frm = new frmMaintainDepartmentQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedDepartment != null)
            {
                txtDepartment.Tag = frm.SelectedDepartment;
                txtDepartment.Text = frm.SelectedDepartment.DepartmentName;
            }
            else
            {
                txtDepartment.Tag = null;
                txtDepartment.Text = "";
            }
        }

        private void txtPosition_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainPositionQuery frm = new frmMaintainPositionQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPosition != null)
            {
                txtPosition.Tag = frm.SelectedPosition;
                txtPosition.Text = frm.SelectedPosition.PositionName;
            }
            else
            {
                txtPosition.Tag = null;
                txtPosition.Text = "";
            }
        }

        private void txtSupervisor_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainEmployeeQuery frm = new frmMaintainEmployeeQuery();
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedPerson != null)
            {
                txtSupervisor.Text = frm.SelectedPerson.NameCn;
                txtSupervisor.Tag = frm.SelectedPerson;
            }
            else
            {
                txtSupervisor.Text = "";
                txtSupervisor.Tag = null;
            }
        }

        private void colSelected_Click(object sender, EventArgs e)
        {
            if (MultiSelected == false)
            {
                return;
            }
            DataRow dr = gridView1.GetDataRow(gridView1.GetSelectedRows()[0]);
            if ((bool)dr["SelectedFlag"])
            {
                dr["SelectedFlag"] = false;
                ((CheckEdit)sender).Checked = false;
            }
            else
            {
                dr["SelectedFlag"] = true;
                ((CheckEdit)sender).Checked = true;
            }
        }

        #endregion
    }
}
